package com.cskaoyan.controller.back;

import com.cskaoyan.bean.bo.back.BrandCreateBO;
import com.cskaoyan.bean.bo.back.BrandUpdateBO;
import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.BrandCreateVO;
import com.cskaoyan.bean.vo.back.BrandUpdateVO;
import com.cskaoyan.config.MagicValue;
import com.cskaoyan.service.back.BrandService;
import com.github.pagehelper.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * 区域、品牌商制造商控制器
 *
 * @author 贾志超
 * @Description:
 * @author: jzc
 * @since: 2022/10/12 10:31
 * @date 2022/10/12
 */
@RestController
@RequestMapping("admin")
public class BrandController {

    @Autowired
    BrandService brandService;


    /**
     * 显示省、市、区列表
     *
     * @return {@link BaseRespVo}
     */

    @GetMapping("region/list")
    public BaseRespVo regionList() {
        CommonData commonData = new CommonData();
        commonData.setPages(MagicValue.COMMONDATA_PAGES);
        commonData.setPage(MagicValue.COMMONDATA_PAGE);
        commonData.setTotal(brandService.getRegionCount());
        commonData.setLimit(brandService.getRegionCount());
        commonData.setList(brandService.getRegionList());
        return BaseRespVo.ok(commonData);
    }

    /**
     * 显示所有品牌商列表
     * 模糊查询
     *
     * @param pageInfo 页面信息
     * @param id       id
     * @param name     名字
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:brand:list")
    @GetMapping("brand/list")
    public BaseRespVo brandList(BasePageInfo pageInfo, Integer id, String name) {
        CommonData commonData = brandService.getbrandList(pageInfo, id, name);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 创建品牌商信息
     *
     * @param brandCreateBO 品牌商创建
     * @return {@link BaseRespVo}
     */
@RequiresPermissions("admin:brand:create")
    @PostMapping("brand/create")
    public BaseRespVo brandCreate(@RequestBody BrandCreateBO brandCreateBO) {
    if (StringUtil.isEmpty(brandCreateBO.getDesc())||brandCreateBO.getFloorPrice()==null){
        return BaseRespVo.isParameter("参数不对");
    }
        BrandCreateVO brandCreateVO = brandService.Createbrand(brandCreateBO);
        return BaseRespVo.ok(brandCreateVO);
    }

    /**
     * 修改品牌商信息
     *
     * @param brandUpdateBO 品牌商更新BO
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:brand:update")
    @PostMapping("brand/update")
    public BaseRespVo brandUpdate(@RequestBody BrandUpdateBO brandUpdateBO) {
        if (StringUtil.isEmpty(brandUpdateBO.getDesc())||brandUpdateBO.getFloorPrice()==null){
            return BaseRespVo.isParameter("参数不对");
        }
        BrandUpdateVO brandUpdateVO =brandService.Updatebrand(brandUpdateBO);
        return BaseRespVo.ok(brandUpdateVO);
    }

    /**
     * 品牌商删除
     *
     * @param brandUpdateBO 品牌商删除BO,复用品牌商更新BO
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:brand:delete")
    @PostMapping("brand/delete")
    public BaseRespVo brandDelete(@RequestBody BrandUpdateBO brandUpdateBO) {
        Integer effected=brandService.Deletebrand(brandUpdateBO);
        if (effected==0){
            BaseRespVo.invalidData();
        }
        return BaseRespVo.ok();
    }

}
